<?php
declare (strict_types = 1);
namespace app\course\logic\offline\admin;

use app\common\exception\LogicException;
use app\course\model\offline\admin\Offline as OfflineModel;
use app\school\model\school\School as SchoolModel;
use app\user\logic\teacher\admin\Teacher;

/**
 * 后台管理逻辑层
 */
class Offline extends OfflineModel
{
    /**
     * 添加课程
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-03-15
     * @param    array  $data 数据
     * @return self
     */
    public function addOffline(array $data)
    {
        // 验证数据
        $this->getValidate()->failException()->check($data);

        $fields = array_keys($this->getFields());

        $data['fullcategorypath'] = $data['cates'];
        $data['category']         = $data['cates'];

        if (!$this->allowField($fields)->data($data, true)->save()) {
            throw new LogicException('添加失败');
        }

        return $this;
    }

    /**
     * 编辑课程
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-03-15
     * @param    array  $data 数据
     * @return   self
     */
    public function editOffline(Offline $offline, array $data)
    {
        // 取得验证器
        $this->getValidate()->failException()->check($data);

        $offline->title            = $data['title'];
        $offline->cover            = $data['cover'];
        $offline->fullcategorypath = implode(',', $data['category']);
        $offline->category         = end($data['category']);
        $offline->mhm_id           = $data['mhm_id'];
        $offline->teacher_id       = $data['teacher_id'];
        $offline->scribing_price   = $data['scribing_price'];
        $offline->price            = $data['price'];
        $offline->status           = $data['status'];
        $offline->section_level    = $data['section_level'];
        $offline->info             = $data['info'];
        if (!$offline->save()) {
            throw new LogicException('编辑失败');
        }

        return $offline;
    }

    /**
     * 编辑指定字段
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-03-27
     */
    public function editField($id, array $data)
    {
        $fields = array_keys($this->getFields());
        $field  = array_keys($data)[0];
        if (!in_array($field, $fields)) {
            throw new LogicException('系统错误');
        }
        // 信息
        $video = $this->findOrEmpty($id);
        if ($video->isEmpty()) {
            throw new LogicException('信息错误');
        }
        $video->$field = $data[$field];
        $video->save();

        $this->cleanCache($video);
        
        // 返回值
        return $video;
    }

    /**
     * 点播列表
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-03-25
     */
    public function offlineList($map)
    {
        $list = $this->with(['cover_url'])->where($map)->paginate($this->getListRows());
        $list = $this->parseList($list);
        return $list;
    }

    /**
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-03-27
     */
    public function parseList($list)
    {
        foreach ($list as $l) {
            $l->teacher_name = app(Teacher::class)->getTeacherNameById($l->teacher_id);
            $l->school_name  = app(SchoolModel::class)->getSchoolNameById($l->mhm_id);
        }
        return $list;
    }

    /**
     * 删除点播
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-03-26
     */
    public function deleteOffline($id)
    {
        $offline = $this->find($id);
        return $offline->delete();
    }
}
